package com.app.controller;


import com.app.common.Result;
import com.app.model.IntegralRecordModel;
import com.app.model.UserModel;
import com.app.service.IntegralRecordService;
import com.app.service.UserService;
import com.app.utils.TokenUtils;
import com.app.vo.IntegralRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 积分记录
 * @author 16693103420
 * @since 2024-01-25
 */
@RestController
@RequestMapping("/integralRecord")
public class IntegralRecordController {
    @Autowired
    private IntegralRecordService IntegralRecordService;
    @Autowired
    private UserService userService;
    //用户查询积分使用记录
    @GetMapping("/getUserIntegralRecordList")
    public Result getUserIntegralRecordList(@RequestParam(defaultValue = "0") Integer page,
                                            @RequestParam(defaultValue = "10") Integer limit, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        Integer userId = userModel.getId();
        IntegralRecordVo integralRecordVo = IntegralRecordService.getUserIntegralRecordList(page, limit,userId);
        return Result.success(integralRecordVo);
    }
    //根据id查询
    @GetMapping("/getIntegralRecordById")
    public Result getIntegralRecordById(Integer id) {
        IntegralRecordModel integralRecord = IntegralRecordService.getById(id);
        return Result.success(integralRecord);
    }

}

